#include "dialogtimeperiodsetting.h"
#include "ui_dialogtimeperiodsetting.h"

#include <QCalendarWidget>

DialogTimePeriodSetting::DialogTimePeriodSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogTimePeriodSetting)
{
    ui->setupUi(this);
    settings.beginGroup("timePeriod");
    ui->spinBoxTimePeriodLength->setValue(settings.value("length",10).toInt());
    ui->dateTimeEditTimePeriodDay->setDate(settings.value("startDate").toDate());
    settings.endGroup();
}

DialogTimePeriodSetting::~DialogTimePeriodSetting()
{
    delete ui;
}

void DialogTimePeriodSetting::on_pushButtonChangePeriodStartDay_clicked()
{
    QCalendarWidget *calendar = new QCalendarWidget();
    calendar->setAttribute(Qt::WA_DeleteOnClose);
    calendar->setWindowTitle(tr("Idő intervallum kezdőnapja"));
    calendar->setSelectedDate(ui->dateTimeEditTimePeriodDay->date());
    calendar->show();

    connect(calendar, SIGNAL(clicked(QDate)), this, SLOT(dateActivated(QDate)));
}

void DialogTimePeriodSetting::on_spinBoxTimePeriodLength_editingFinished()
{
    settings.beginGroup("timePeriod");
    settings.setValue("length", ui->spinBoxTimePeriodLength->value());
    settings.endGroup();
}

void DialogTimePeriodSetting::dateActivated(QDate date)
{
    settings.beginGroup("timePeriod");
    settings.setValue("startDate", date);
    ui->dateTimeEditTimePeriodDay->setDate(date);
    settings.endGroup();
}
